package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Constants;
import system.Device;

import com.game.R;

public class Menu implements Constants{
	
	private int m_iMaxOfItem;
	private int m_itexBackgroundID;
	private int m_itexTitleID;
	private int m_iSelectedItem = 0;
	
	private int[] m_iTexMenuItem;
	private int[] m_iTexMenuItemSelected;
	private int m_iCurItem = 0;
	private int m_iMenuItemWidth = (int)Device.MENU_BUTTON_WIDTH;
	private int m_iMenuItemHeight = (int)Device.MENU_BUTTON_HEIGHT;
	private GL10 gl;
	
	private TexSquare m_menuItem;
	private TexSquare m_gameName;
	private TexSquare m_mainMenuBg;
	private Texture m_texture;
	
	private float m_fSrcWidth;
	private float m_fSrcHeight;
	private float m_iGameNameWidth = 350;
	private float m_iGameNameHeight = 60;
	
	private boolean m_isUseBGColor = false;
	private float m_fBgRed = 0;
	private float m_fBgGreen = 0;
	private float m_fBgBlue = 0;
	private boolean m_isUseDefaultTitle = true;
	
	@SuppressWarnings("unused")
	private boolean m_isEnableScaleItem = true;
	@SuppressWarnings("unused")
	private boolean m_isEnableScaleTitle = true;
	
	public Menu(GL10 gl, int maxOfItem, int menuItemWidth, int menuItemHeight)
	{
		m_iMaxOfItem = maxOfItem;
		m_iMenuItemWidth = menuItemWidth;
		m_iMenuItemHeight = menuItemHeight;
		
		m_iTexMenuItem = new int[maxOfItem];
		m_iTexMenuItemSelected = new int[maxOfItem];
		m_isUseBGColor = false;
		
		this.gl = gl;
		
		Init();
	}
	
	public void SetBackgroundColor(float red, float green, float blue)
	{
		m_fBgRed = red;
		m_fBgGreen = green;
		m_fBgBlue = blue;
	}
	
	private void Init()
	{
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		
		Device.DisableScaleFactor();
		m_mainMenuBg = new TexSquare(m_fSrcWidth, m_fSrcHeight);
		m_menuItem = new TexSquare(m_iMenuItemWidth, m_iMenuItemHeight);
		Device.EnableScaleFactor();
		m_gameName = new TexSquare(m_iGameNameWidth, m_iGameNameHeight);
		
		m_iMenuItemWidth = (int) m_menuItem.GetWidth();
		m_iMenuItemHeight = (int) m_menuItem.GetHeight();
		
		m_iGameNameWidth = m_gameName.GetWidth();
		m_iGameNameHeight = m_gameName.GetHeight();
		
		// Background
		m_texture = new Texture();
		m_itexBackgroundID = m_texture.initTexture(R.drawable.mainmenu_bg);
		// GameName
		m_itexTitleID = m_texture.initTexture(R.drawable.gamename);	
	}
	
	public void SetEnableScaleItem(boolean isEnable)
	{
		m_isEnableScaleItem = isEnable;
		if(!isEnable) Device.DisableScaleFactor();
		m_menuItem = new TexSquare(m_iMenuItemWidth, m_iMenuItemHeight);
		m_iMenuItemWidth = (int) m_menuItem.GetWidth();
		m_iMenuItemHeight = (int) m_menuItem.GetHeight();
		Device.EnableScaleFactor();
		
	}
	
	public void SetEnableScaleTitle(boolean isEnable)
	{
		m_isEnableScaleTitle = isEnable;
		if(!isEnable) Device.DisableScaleFactor();
		m_gameName = new TexSquare(m_iGameNameWidth, m_iGameNameHeight);
		m_iGameNameWidth = m_gameName.GetWidth();
		m_iGameNameHeight = m_gameName.GetHeight();
		Device.EnableScaleFactor();
	}
	
	public void AddMenuItem(int textID, int texSelectedID)
	{
		m_iTexMenuItem[m_iCurItem] = textID;
		m_iTexMenuItemSelected[m_iCurItem++] = texSelectedID;
	}
	
	public void SetMenuItemSize(int width, int height)
	{
		m_iMenuItemWidth = width;
		m_iMenuItemHeight = height;
	}
	
	public void SetBackground(int texBackgroundID)
	{
		m_itexBackgroundID = texBackgroundID;
	}
	
	public void SetTitle(int texTitleID, int width, int height)
	{
		m_itexTitleID = texTitleID;
		m_iGameNameWidth = width;
		m_iGameNameHeight = height;
		
		m_gameName = new TexSquare(width, height);
		
		m_isUseDefaultTitle = false;
	}
	
	public void SetSelectedMenuItem(int index)
	{
		m_iSelectedItem = index;
	}
	
	public void SetUseBgColor(boolean isUseBgColor)
	{
		m_isUseBGColor = isUseBgColor;
	}
	
	public void SetUseDefaultTitle(boolean isUseDefaultTitle)
	{
		m_isUseDefaultTitle = isUseDefaultTitle;
	}
	
	public void Render()
	{
		if(m_isUseBGColor)
		{
			gl.glClearColor(m_fBgRed, m_fBgGreen, m_fBgBlue, 1.0f);
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		}
		else
		{
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			
			// Back ground
			gl.glPushMatrix();
			gl.glLoadIdentity();
			m_texture.loadTexture(m_itexBackgroundID);
			m_mainMenuBg.Render();
		}
		
		// Title
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		
		gl.glPushMatrix();
		if(m_isUseDefaultTitle)
			gl.glTranslatef(0, (m_fSrcHeight/2 + (m_iMaxOfItem/2 - 1) * m_iMenuItemHeight)/2, 0);
		else
			gl.glTranslatef(0, m_fSrcHeight/4, 0);
		m_texture.loadTexture(m_itexTitleID);
		m_gameName.Render();
		gl.glPopMatrix();
		
		// Draw menuItem
		for(int i = 0; i < m_iMaxOfItem; ++i)
		{			
			gl.glPushMatrix();
			gl.glTranslatef(0, (m_iMaxOfItem/2 - i - 1) * m_iMenuItemHeight, 0);
			if(m_iSelectedItem == i)
				m_texture.loadTexture(m_iTexMenuItemSelected[i]);
			else
				m_texture.loadTexture(m_iTexMenuItem[i]);
			m_menuItem.Render();
			gl.glPopMatrix();
		}
		
		gl.glPopMatrix();
	}
	
	public void Clear() {
		m_menuItem = null;
		m_mainMenuBg = null;
		m_gameName = null;
		m_iTexMenuItem = null;
		m_iTexMenuItemSelected = null;
	}
}
